package com.shoulder.boot.acl.service.impl;

import com.shoulder.boot.acl.service.CaptchaService;
import com.shoulder.boot.acl.vo.CaptchaVo;
import com.shoulder.captcha.ArithmeticCaptcha;
import com.shoulder.captcha.base.Captcha;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@AllArgsConstructor
@Service
public class CaptchaServiceImpl implements CaptchaService {
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public CaptchaVo create() {
        Captcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);

        String text = captcha.text();
        String base64 = captcha.toBase64();
        CaptchaVo vo = new CaptchaVo();
        vo.setId(UUID.randomUUID().toString());
        vo.setUrl(base64);
        redisTemplate.opsForValue().set(vo.getId(), text, 3, TimeUnit.MINUTES);
        return vo;
    }
}
